<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="addForm">
                    <el-form-item label="是否隐藏" :label-width="formLabelWidth">
                        <el-switch v-model="show" active-color="#13ce66">
                        </el-switch>
                    </el-form-item>
                    <el-form-item label="广告位" prop="type" :label-width="formLabelWidth">
                        <el-select v-model="addForm.advertisingPlace" placeholder="请选择广告位">
                            <el-option v-for="(type,index) in types" :key="index" :label="type" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="首页图片" :label-width="formLabelWidth">
                        <el-upload class="avatar-uploader" :data="uploadImg" ref="upload" :action="getLocation" :show-file-list="false" :on-success="handleAvatarSuccess" v-loading.fullscreen.lock="fullscreenLoading" :before-upload="beforeAvatarUpload">
                            <img v-if="addForm.imgUrl" :src="addForm.imgUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel(addForm)">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <!-- <el-input v-model="select_word" placeholder="输入机构名" @click="search" class="search"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button> -->
            <el-button type="primary" size="small" @click="addEx">新增图片</el-button>
        </div>
        <div>
            <el-table :data="pics.content" border fit style="width: 100%">
                <el-table-column label="图片">
                    <template slot-scope="scope">
                        <img :src="scope.row.imgUrl" width="100%" height="100%" />
                    </template>
                </el-table-column>
                <el-table-column prop="addTime" label="添加时间">
                </el-table-column>
                <el-table-column prop="state" :formatter="formatStatus" label="状态">
                </el-table-column>
                <el-table-column prop="state" :formatter="formatStatus" label="广告位">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="editPic(scope.row)" size="small">编辑</el-button>
                        <el-button type="text" @click="delPic(scope.row)" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="pics.number+1" layout="total, sizes, prev, pager, next, jumper" :total="pics.totalElements">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { indexPic, addIndexPic, delIndexPic, allPics, putIndexPic } from '../../server/getDate.js'
import { mapState, mapActions } from 'vuex'
import { location } from '../../utils/local.js'
export default {
    data() {
        return {
            page: {

            },
            addForm: {},
            dialogFormVisible: false,
            title: '',
            getLocation: '',
            formLabelWidth: '120px',
            uploadImg: {},
            show: false,
            fullscreenLoading: false,
            types: ['头部', '中上', '中部', '中下']

        }
    },
    created() {
        this.getIndexPic()
        this.getLocation = location
    },
    computed: {
        ...mapState({
            pics: state => state.File.pics,
            user: state => state.Login.user
        })
    },
    methods: {
        ...mapActions(['getIndexPics']),
        getIndexPic() {
            allPics().then(res => {
                console.log('res', res)
                if (res.status === 0) {
                    this.getIndexPics(res.result)
                } else {
                    this.$message.error(res.message)
                }
            }, err => this.$message.error('服务器异常'))
        },
        add() {
            let titleImgEntity = this.addForm
            let userId = this.user.id
            console.log('this.show', titleImgEntity)
            titleImgEntity.state = this.show ? 0 : 1
            if (titleImgEntity.id) {
                putIndexPic({ titleImgEntity, userId })
                    .then(res => {
                        console.log('res')
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.$message.info('修改成功')
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            } else {
                addIndexPic({ titleImgEntity, userId })
                    .then(res => {
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.getIndexPic()
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }
        },
        addEx() {
            this.dialogFormVisible = true
            this.title = '添加图片'
            this.addForm = {}
            this.show = false
        },
        editPic(row) {
            console.log('row', row.state)
            this.dialogFormVisible = true
            this.title = '修改图片'
            this.addForm = row
            this.show = row.state == 1 ? false : true
        },
        delPic(row) {
            let id = row.id
            delIndexPic({ id })
                .then(res => {
                    if (res.status === 0) {
                        this.$message.info('删除成功')
                        this.getIndexPic()
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('服务器异常'))

        },
        formatStatus(row, col) {
            return row.state == 1 ? '展示' : "不展示"
        },
        beforeAvatarUpload(file) {
            return this.upload(file)
        },
        handleAvatarSuccess(res, file) {
            console.log('上传图片', res)
            this.addForm.imgUrl = res.result
            // this.show = !this.show
            // this.show = !this.show
            this.fullscreenLoading = false
            console.log("this.addForm", this.addForm)
        },
        upload(file) {
            this.fullscreenLoading = true
            this.uploadImg.type = 0
            this.uploadImg.file = file
        },
        handleCurrentChange(val) {
            let index = val
            let size = this.page.size
            allPics({ index, size })
                .then(res => {
                    if (res.status === 0) {
                        this.getIndexPic(res.result)
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('服务器异常')

                )
        },
        cancel(addForm) {
            this.dialogFormVisible = false
            this.getIndexPic()
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
  .expert {
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>